package cn.hx.handsetticketinspection.listener;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import cn.hx.handsetticketinspection.base.BaseBean;
import cn.hx.handsetticketinspection.constant.HttpUrls;
import cn.hx.handsetticketinspection.netword.HttpUtils;
import cn.hx.handsetticketinspection.netword.Interfaceabstract.HttpInterface;
import cn.hx.handsetticketinspection.netword.Interfaceabstract.OKHttpListener;
import cn.hx.handsetticketinspection.params.DeviceStateReport;
import cn.hx.handsetticketinspection.utils.Utils;

/**
 * 监听当前的App设备程序是前台运行还是后台运行
 */
public class ActivityLifecycleListener implements Application.ActivityLifecycleCallbacks, HttpInterface {
    private int refCount = 0;
    private int oldCount;
    Activity activity;
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        this.activity = activity;
    }

    @Override
    public void onActivityStarted(Activity activity) {
        this.activity = activity;
        refCount++;
        if (oldCount==0){
            DeviceStateReport report = new DeviceStateReport();
            report.setStateType("run");
            report.setStateValue(1);
            HttpUtils.postDefault(this, HttpUrls.basic_deviceStateReport_stateReport,
                    report.toMapUtils(),
                    BaseBean.class, new OKHttpListener<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean bean) {
                            // Utils.toast(bean.response);
                        }
                    });
        }
        oldCount=1;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        this.activity = activity;

    }

    @Override
    public void onActivityPaused(Activity activity) {
        this.activity = activity;

    }

    @Override
    public void onActivityStopped(Activity activity) {
        this.activity = activity;
        refCount--;
        if(refCount == 0){
            DeviceStateReport report = new DeviceStateReport();
            report.setStateType("run");
            report.setStateValue(3);
            HttpUtils.postDefault(this, HttpUrls.basic_deviceStateReport_stateReport,
                    report.toMapUtils(),
                    BaseBean.class, new OKHttpListener<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean bean) {
                            // Utils.toast(bean.response);
                        }
                    });

            oldCount=0;
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        this.activity = activity;
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        this.activity = activity;
    }

    @Override
    public Activity getActivity() {
        return this.activity ;
    }

    @Override
    public boolean isDiscardHttp() {
        return true;
    }
}
